#!/bin/bash
# Hilfsmittel zur Erstllung eines eigenen VPNs!

usage() {
cat << EOF
usage: $0 options

This script installs a simple tun/tap device and .
WARNING: This script needs sudo permissions.

OPTIONS:
   -h      Show this message
   -t      type: either "tun" or "tap" (std = "tap")
   -i      tun/tap device name (std = "tap01")
   -a      tun/tap device address (std = "10.0.0.1")
EOF
}

TYPE=tap
TAP=tap01
IP=10.0.0.1

while getopts “hi:a:t:” OPTION
do
     case $OPTION in
         h)
             usage
             exit 1
             ;;
         i)
             TAP=$OPTARG
             ;;
         a)
             IP=$OPTARG
             ;;
         t)
             TYPE=$OPTARG
             ;;
         ?)
             usage
             exit
             ;;
     esac
done

if [[ -z $TAP ]] || [[ -z $IP ]] || [[ -z $TYPE ]]
then
     usage
     exit 1
fi

# set up TUN/TAP-Interface ...
sudo ip tuntap add $TAP mode $TYPE
sudo ip link set $TAP up
sudo ip addr add $IP/24 dev $TAP

# set up pipes ...
mkfifo to_tap
mkfifo from_tap

# reading/writing data from pipe/to pipe ...
if [[ tun = $TYPE ]]
then
echo "reading from tun device..."
./rwtuntap -d -u -i $TAP to_tap from_tap &
elif [[ tap = $TYPE ]]
then
echo "reading from tap device..."
./rwtuntap -d -a -i $TAP to_tap from_tap &    
fi
